package com.saver.core.persistence;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.saver.core.domain.Banco;
import com.saver.core.persistence.table.BancoTable;
import com.saver.core.util.Constantes;

public class BancoSQLite extends DatabaseHelper {
	
	private static final String TAG = "BancoSQLite";
	private static final String[] COLUMNAS = new String []{BancoTable.COL_ID_BANCO, 
		BancoTable.COL_NOMBRE, BancoTable.COL_NUMERO, BancoTable.COL_ICONO, BancoTable.COL_URL,
		BancoTable.COL_ESTADO};

	public BancoSQLite(Context context) {
		super(context, Constantes.DATABASE_NAME, null, Constantes.DATABASE_VERSION);
	}
	
	public List<Banco> buscarTodoActivo () {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(BancoTable.TABLA, COLUMNAS, "ESTADO=?", new String[]{"1"}, null, null, BancoTable.COL_NOMBRE);
		ArrayList<Banco> bancos = new ArrayList<Banco>();
		while (cursor.moveToNext()) {
			Banco banco = crearBanco(cursor);
			bancos.add(banco);
		}
		db.close();
		return bancos;
	}

	private Banco crearBanco(Cursor cursor) {
		Banco banco = new Banco();
		banco.setId(cursor.getInt(0));
		banco.setNombre(cursor.getString(1));
		banco.setNumero(cursor.getString(2));
		banco.setIcono(cursor.getString(3));
		banco.setUrl(cursor.getString(4));
		banco.setEstado(cursor.getInt(5));
		return banco;
	}
	
	public Banco buscarPorNumero (String numero) {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(BancoTable.TABLA, COLUMNAS, BancoTable.COL_NUMERO + "=?", new String[]{numero}, null, null, null);
		Banco banco = null;
		if (cursor.moveToNext()) {
			banco = crearBanco(cursor);
		}
		return banco;
	}

}
